package com.vanke.devops.api.controller.zuulroute;

import com.vanke.center.common.enums.CommonStatusEnum;
import com.vanke.center.common.resp.BaseResponse;
import com.vanke.core.bean.ApiBaseResponse;
import com.vanke.core.bean.PagingResponse;
import com.vanke.devops.api.ZuulRouteApi;
import com.vanke.devops.api.base.AbstractController;
import com.vanke.devops.api.base.BaseController;
import com.vanke.devops.api.controller.zuulroute.form.ZuulRouteForm;
import com.vanke.devops.dal.model.ZuulRoute;
import com.vanke.devops.domain.domain.zuulroute.IZuulRouteService;
import com.vanke.devops.domain.domain.zuulroute.param.ZuulRouteParam;
import com.vanke.devops.dto.ZuulRouteDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表  前端控制器
 * </p>
 *
 * @author 陈景阳
 * @since 2018-05-11
 */
@RestController
public class ZuulRouteController extends AbstractController<IZuulRouteService> implements ZuulRouteApi {

    /**
     * 分页列表
     */
    @GetMapping(value = "/page/{pageSize}/{pageNo}")
    public PagingResponse list(@PathVariable("pageSize") Integer pageSize,
                               @PathVariable("pageNo") Integer pageNo,
                               @RequestParam("keyword") String keyword)  {
        return setResponse(service.page(pageNo,pageSize,keyword));
    }

    /**
     * 获取路由信息
     */
    @GetMapping("/{id}")
    public ApiBaseResponse get(@PathVariable("id") Integer id)  {
        return setResponse(service.getInfo(id));
    }

    /**
     * 更新路由
     */
    @PutMapping("/{id}")
    public ApiBaseResponse update(@Valid @RequestBody ZuulRouteForm form, @PathVariable("id") Integer id)  {
        ZuulRouteParam param = new ZuulRouteParam();
        BeanUtils.copyProperties(form,param);
        return setResponse(service.update(id,param));
    }

    /**
     * 删除路由
     */
    @DeleteMapping("/{id}")
    public ApiBaseResponse delete(@PathVariable("id") Integer id)  {
        service.delete(id);
        return setResponse();
    }

    /**
     * 新增路由
     */
    @PostMapping
    public ApiBaseResponse add(@Valid @RequestBody ZuulRouteForm form)  {
        ZuulRouteParam param = new ZuulRouteParam();
        BeanUtils.copyProperties(form,param);
        return setResponse(service.add(param));
    }

    /**
     * 获取所有的路由信息
     */
    @Override
    public BaseResponse<List<ZuulRouteDTO>> all() {
        return  BaseResponse.create(service.all(), CommonStatusEnum.SUCCESS);
    }

    /**
     * 发布路由
     */
    @GetMapping("/publish")
    public ApiBaseResponse publish(){
        service.publish();
        return setResponse();
    }
}